<script type="text/javascript"><!--//--><![CDATA[//><!--

    $(document).ready(function() {
        // Setup Form
		$('#form_master').setupForm([
            { name: 'no_po', type: 'text', size: 'long', maxlength: 100, noempty:true },
			{ name: 'kd_vendor', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama_vendor').setval(''); 
				},
				onActionButton:function(){ 
					LOVVendor({ onSelect:function(row){ 
						$('#kd_vendor').setval(row.kd_vendor); 
						$('#nama_vendor').setval(row.nama_vendor); 
					} }); 
				}
			},
			{ name: 'nama_vendor', type: 'text', size: '24', readonly:true },
			{ name: 'jenis_pembayaran', type: 'text', size: 'long', maxlength: 100, noempty:true },
			{ name: 'kode_barang_add', type: 'text', size: '24' },
			{ name: 'nama_barang_view', type: 'text', size: '24', readonly:true },
			{ name: 'jumlah_barang', type: 'text', size: '24' },
			
			
        ], <?php echo json_encode($data); ?>);
        
		
        <?php if($mode=="edit"){ ?>
        $('#no_po').readonly();
        <?php } ?>
		
		// Set Action Button
		var buttons = [{ label: 'Save', icon: 'save', onclick: function() { 
							$('#form_master').submitForm(	'/'+controller+'/ajax_post', 
							{ detail:[	{ table: 'dtl_sample2', field:['kd_barang','qty_order','no_po']}																	], 
							onComplete : function(data){ eval('oData = '+data); loadPage('/'+controller+'/edit/?no_po='+(oData.genid!='' ? oData.genid : $('#no_po').getval())); }
															} 
														); 
						}},
					   { label: 'Close', icon: 'cancel', onclick: function() { loadPage('/'+controller+'/Index'); } }];
		setAction(buttons);
		
		// Create Tab
		$('#tt').tabs({ border:false });  
		
		// Focus at First Object
		$('#form_master').focusFirst();

		$("#kode_barang_add").autocomplete({
	      minLength: 0,
	      source: function (request, response) {
	      		var getKB = $('#kode_barang_add').val();
		        $.getJSON(gs_path+'/km_barang/getdata_ac/' + getKB , function (data) {
		            response($.map(data['rows'], function (isi, index) {
		                return {
		                	desc: isi['desc'],
		                    label: isi['label'],
		                    value: isi['value'],
		                    supplier_kode: isi['supplier_kode'],
		                    supplier_nama: isi['supplier_nama']
		                };
		            }));
		        });
		    },
		  focus: function( event, ui ) {
	        $("#kode_barang_add").val(ui.item.label);
	        return false;
	      },
	      select: function( event, ui ) {
	        $( "#kode_barang_add" ).val( ui.item.label );
	        $( "#nama_barang_view" ).val( ui.item.desc );
	        $( "#supplier_nama" ).val( ui.item.supplier_nama );
	        $('#jumlah_barang').val('1');
	        $('#jumlah_barang').focus();
	 		
	        return false;
	      }
	    })
	    .keypress(function (e,item) {
            if (e.which == 13) {
            	var getKB = $('#kode_barang_add').val();

            	if(getKB == ''){
            		$('#bayar').val('');
            		$('#bayar').focus();
            	}

            	var json = (function () {
				    var json = null;
				    $.ajax({
				        'async': false,
				        'global': false,
				        'url': gs_path+'/km_barang/getdata_ac/' + getKB,
				        'dataType': "json",
				        'success': function (data) {
				            json = data;
				        }
				    });

				    if(json['rows'].length == 1){
				    	$( "#kode_barang_add" ).val(json['rows'][0]['label']);
					    $( "#nama_barang" ).val(json['rows'][0]['desc']);
					    $( "#harga_barang" ).val(json['rows'][0]['value']);
					    $('#jumlah_barang').val('1');
					    $( "#sub_total_barang" ).val(json['rows'][0]['value']);
					    $('#jumlah_barang').focus();
				    }
				    return json;
				})();
            }
        })
	    .data("autocomplete")._renderItem = function(ul,item) {
	      return $("<li>").data('item.autocomplete', item).append("<a>" + item.label + " - " + item.desc + "</a>").appendTo(ul);
	    };

	    $('#kode_barang_add').focus();

    });
//--><!]]></script>

<div id="form_master_message" style="display:none;"></div>
<table class="tb_form" style="display:none;" cellpadding="0" cellspacing="0">

<!-- <tr>
    <td class="label">No PO</td>
    <td><input id="no_po" /></td>
</tr>
<tr>
    <td class="label">Tanggal PO</td>
    <td><input id="tgl_order" /></td>
</tr>

<tr>
	<td class="label">Supplier</td>
    <td><input id="kd_vendor" /><input id="nama_vendor" /></td>
</tr>
<tr>
    <td class="label">Jenis Pembayaran</td>
    <td><input id="jenis_pembayaran" /></td>
</tr> -->
</table>
<div style="min-height:300px;">
    <?php require_once('formdtl_sample2.php'); ?>
</div>